package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcNursingShiftChangeRecordMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.ChangeStatusConstant;
import com.csun.cmny.provider.model.constant.OmcConstant;
import com.csun.cmny.provider.model.domain.OmcNursingShiftChange;
import com.csun.cmny.provider.model.domain.OmcNursingShiftChangeRecord;
import com.csun.cmny.provider.model.domain.OmcNursingShiftChangeRecordRemark;
import com.csun.cmny.provider.model.dto.nursingShiftChangeRecord.NursingChangeSaveDto;
import com.csun.cmny.provider.model.dto.nursingShiftChangeRecord.NursingShiftChangeRecordSelectDto;
import com.csun.cmny.provider.model.dto.nursingShiftChangeRecord.NursingShiftSaveDto;
import com.csun.cmny.provider.model.dto.nursingShiftChangeRecord.OmcNursingShiftChangeRecordSaveDto;
import com.csun.cmny.provider.model.vo.nursingShiftChangeRecord.NursingShiftChangeRecordParticularsVo;
import com.csun.cmny.provider.model.vo.nursingShiftChangeRecord.NursingShiftChangeRecordVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcNursingShiftChangeRecordRemarkService;
import com.csun.cmny.provider.service.OmcNursingShiftChangeRecordService;
import com.csun.cmny.provider.service.OmcNursingShiftChangeService;
import com.csun.cmny.provider.utils.JPushService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcNursingShiftChangeRecordServicecImpl extends BaseService<OmcNursingShiftChangeRecord> implements OmcNursingShiftChangeRecordService {

    @Resource
    private OmcNursingShiftChangeRecordMapper shiftChangeRecordMapper;

    @Resource
    private OmcNursingShiftChangeRecordRemarkService remarkService;

    @Resource
    private OmcNursingShiftChangeService shiftChangeService;

    @Resource
    private JPushService jpushService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;




    @Override
    public int addNursingShiftChangeRecord(OmcNursingShiftChangeRecordSaveDto shiftChangeRecordSaveDto, LoginAuthDto loginAuthDto) {

        //写入交接班记录
        OmcNursingShiftChangeRecord omcNursingShiftChangeRecord = new OmcNursingShiftChangeRecord();
        BeanUtils.copyProperties(shiftChangeRecordSaveDto,omcNursingShiftChangeRecord);

        omcNursingShiftChangeRecord.setGroupId(loginAuthDto.getGroupId());
        omcNursingShiftChangeRecord.setNursingUserId(loginAuthDto.getUserId());
        omcNursingShiftChangeRecord.setNursingName(loginAuthDto.getUserName());

        int insert = save(omcNursingShiftChangeRecord);

        //写入交接情况
        for (OmcNursingShiftChangeRecordRemark omcNursingShiftChangeRecordRemark : shiftChangeRecordSaveDto.getRemarks()) {
            omcNursingShiftChangeRecordRemark.setRecordId(omcNursingShiftChangeRecord.getId());

            remarkService.save(omcNursingShiftChangeRecordRemark);
        }

        return insert;
    }

    @Override
    public List<NursingShiftChangeRecordVo> selectRecord(NursingShiftChangeRecordSelectDto nursingShiftChangeRecordSelectDto) {
        return shiftChangeRecordMapper.selectRecord(nursingShiftChangeRecordSelectDto);
    }

    @Override
    public List<NursingShiftChangeRecordParticularsVo> selectNursingShiftChangeRecordById(Long id) {

        //查询交接记录
        OmcNursingShiftChangeRecord omcNursingShiftChangeRecord = selectByKey(id);

        NursingShiftChangeRecordParticularsVo nursingShiftChangeRecordParticularsVo = new NursingShiftChangeRecordParticularsVo();

        BeanUtils.copyProperties(omcNursingShiftChangeRecord,nursingShiftChangeRecordParticularsVo);

        OmcNursingShiftChange omcNursingShiftChange = shiftChangeService.selectByKey(omcNursingShiftChangeRecord.getShiftChangeId());
        nursingShiftChangeRecordParticularsVo.setBudingNo(omcNursingShiftChange.getBudingNo());

        //写入交接记录详情
        OmcNursingShiftChangeRecordRemark nursingShiftChangeRecordRemark = new OmcNursingShiftChangeRecordRemark();
        nursingShiftChangeRecordRemark.setRecordId(id);
        List<OmcNursingShiftChangeRecordRemark> remarks = remarkService.select(nursingShiftChangeRecordRemark);
        nursingShiftChangeRecordParticularsVo.setRemarks(remarks);

        ArrayList<NursingShiftChangeRecordParticularsVo> vos = new ArrayList<>();
        vos.add(nursingShiftChangeRecordParticularsVo);
        return vos;
    }

    @Override
    public int addNursingShift(NursingShiftSaveDto shiftSaveDto, LoginAuthDto loginAuthDto) {
        //新增交接班
        OmcNursingShiftChange omcNursingShiftChange = new OmcNursingShiftChange();
        BeanUtils.copyProperties(shiftSaveDto,omcNursingShiftChange);
        omcNursingShiftChange.setShiftNursingName(loginAuthDto.getUserName());
        omcNursingShiftChange.setShiftNursingId(loginAuthDto.getUserId());
        int save = shiftChangeService.save(omcNursingShiftChange);

        //写入交接班记录
        OmcNursingShiftChangeRecord omcNursingShiftChangeRecord = new OmcNursingShiftChangeRecord();
        BeanUtils.copyProperties(shiftSaveDto,omcNursingShiftChangeRecord);

        omcNursingShiftChangeRecord.setShiftChangeId(omcNursingShiftChange.getId());
        omcNursingShiftChangeRecord.setChangeStatus(ChangeStatusConstant.SHIFT);
        omcNursingShiftChangeRecord.setGroupId(loginAuthDto.getGroupId());
        omcNursingShiftChangeRecord.setNursingUserId(loginAuthDto.getUserId());
        omcNursingShiftChangeRecord.setNursingName(loginAuthDto.getUserName());

        int insert = save(omcNursingShiftChangeRecord);

        //写入交接情况
        for (OmcNursingShiftChangeRecordRemark omcNursingShiftChangeRecordRemark : shiftSaveDto.getRemarks()) {
            omcNursingShiftChangeRecordRemark.setRecordId(omcNursingShiftChangeRecord.getId());

            remarkService.save(omcNursingShiftChangeRecordRemark);
        }

        //推送给护工
        String message = JSONObject.fromObject(omcNursingShiftChange).toString();

        List<String> tagListNursingUser = Lists.newArrayList();

        tagListNursingUser.add(OmcConstant.NURSING_PREFIX+shiftSaveDto.getChangeNursingId());


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(new Date());
        String[] nursingName = {loginAuthDto.getUserName(),date};

        //呼叫推送给护工
        jpushService.sendToHualongNursing(cmnyMessageSource.getMessage("nursing.shift.change", nursingName),
                cmnyMessageSource.getMessage("nursing.shift.change", nursingName),
                cmnyMessageSource.getMessage("nursing.shift.change", nursingName),
                message,
                tagListNursingUser);
        return insert;
    }

    @Override
    public int addNursingChange(NursingChangeSaveDto changeSaveDto, LoginAuthDto loginAuthDto) {

        //判断交接班情况和交班人是否对应
        OmcNursingShiftChangeRecord omcNursingShiftChangeRecord = new OmcNursingShiftChangeRecord();
        omcNursingShiftChangeRecord.setShiftChangeId(changeSaveDto.getShiftChangeId());
        List<OmcNursingShiftChangeRecord> select = select(omcNursingShiftChangeRecord);

        if (PublicUtil.isEmpty(select)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032044);
        }
        if (select.size()>1){
            throw new OmcBizException(ErrorCodeEnum.OMC10032045);
        }

        OmcNursingShiftChangeRecordRemark nursingShiftChangeRecordRemark = new OmcNursingShiftChangeRecordRemark();
        nursingShiftChangeRecordRemark.setRecordId(select.get(0).getId());
        List<OmcNursingShiftChangeRecordRemark> shiftRemarks = remarkService.select(nursingShiftChangeRecordRemark);

        ArrayList<Long> oldManId1 = new ArrayList<>();
        for (OmcNursingShiftChangeRecordRemark shiftRemark : shiftRemarks) {
            oldManId1.add(shiftRemark.getOldManId());
        }
        ArrayList<Long> oldManId2 = new ArrayList<>();
        for (OmcNursingShiftChangeRecordRemark omcNursingShiftChangeRecordRemark : changeSaveDto.getRemarks()) {
            oldManId2.add(omcNursingShiftChangeRecordRemark.getOldManId());
        }
        for (int i = 0; i < oldManId1.size(); i++) {
            if (!oldManId2.contains(oldManId1.get(i))){
                String[]oldManNames={shiftRemarks.get(i).getOldManName()};
                throw new OmcBizException(ErrorCodeEnum.OMC10032046,oldManNames);
            }
        }


        //写入交接班记录
        BeanUtils.copyProperties(changeSaveDto,omcNursingShiftChangeRecord);


        omcNursingShiftChangeRecord.setChangeStatus(ChangeStatusConstant.CHANGE);
        omcNursingShiftChangeRecord.setGroupId(loginAuthDto.getGroupId());
        omcNursingShiftChangeRecord.setNursingUserId(loginAuthDto.getUserId());
        omcNursingShiftChangeRecord.setNursingName(loginAuthDto.getUserName());
        int insert = save(omcNursingShiftChangeRecord);

        for (OmcNursingShiftChangeRecordRemark omcNursingShiftChangeRecordRemark : changeSaveDto.getRemarks()) {
            omcNursingShiftChangeRecordRemark.setRecordId(omcNursingShiftChangeRecord.getId());
        }


        //写入交接情况
        remarkService.batchSave(changeSaveDto.getRemarks());
        return insert;
    }
}
